package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.utils.thread.WmThreadLocalUtil;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.UUID;

@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Autowired
    private FileStorageService fileStorageService;

    @Autowired
    private WmMaterialMapper wmMaterialMapper;

    /**
     * 图片上传
     *
     * @param multipartFile
     * @return {@link ResponseResult}<{@link WmMaterial}>
     */
    @Override
    public ResponseResult<WmMaterial> loadImage(MultipartFile multipartFile) {
        //todo 1.检查参数
        if (multipartFile == null || multipartFile.getSize() == 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
         //todo 2.上传到minio
        //定义存储到minio的文件名
        String firstName = UUID.randomUUID().toString().replace("-","");
        //获取后缀名 --- 从原文件名中获取
        String originalFilename = multipartFile.getOriginalFilename();
        String lastName = originalFilename.substring(originalFilename.lastIndexOf("."));
        String fileName = firstName + lastName;
        String url = null;
        try {
            url = fileStorageService.uploadHtmlFile("", fileName, multipartFile.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
            log.error("文件上传失败");
        }
        //todo 3.将minio的地址保存到数据库中
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setUserId(WmThreadLocalUtil.getUser().getId());
        wmMaterial.setUrl(url);
        wmMaterial.setType((short) 0);
        wmMaterial.setIsCollection((short) 0);
        wmMaterial.setCreatedTime(new Date());
        //wmMaterialMapper.insert(wmMaterial);
        save(wmMaterial);
        //todo 4.封装返回
        return ResponseResult.okResult(wmMaterial);
    }

    /**
     * 素材列表查询
     *
     * @param wmMaterialDto
     * @return {@link ResponseResult}<{@link WmMaterial}>
     */
    @Override
    public ResponseResult findList(WmMaterialDto wmMaterialDto) {
        //todo 1.检查参数
        wmMaterialDto.checkParam();
        //todo 2. 分页查询
        //条件:是否收藏,按照用户查询,按照时间倒序排序
        IPage page = new Page(wmMaterialDto.getPage(),wmMaterialDto.getSize());
        LambdaQueryWrapper<WmMaterial> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (wmMaterialDto.getIsCollection() != null && wmMaterialDto.getIsCollection() == 1) {
            lambdaQueryWrapper.eq(WmMaterial::getIsCollection,wmMaterialDto.getIsCollection());
        }
        //按照用户查询
        lambdaQueryWrapper.eq(WmMaterial::getUserId,WmThreadLocalUtil.getUser().getId());
        //按照时间倒序排序
        lambdaQueryWrapper.orderByDesc(WmMaterial::getCreatedTime);
        IPage Ipage = baseMapper.selectPage(page, lambdaQueryWrapper);

        //todo 3.结果返回
        ResponseResult responseResult = new PageResponseResult(wmMaterialDto.getPage(),wmMaterialDto.getSize(), (int) Ipage.getTotal());
        responseResult.setData(Ipage.getRecords());
        return responseResult;
    }
}
